// 阶乘
function factorial (num) {
  if (num <= 1) {
    return 1
  } else {
    return num * factorial(num - 1)
  }
}

/*
 * 生成贝塞尔曲线插值点
 * @para n {number} 控制点数量
 * @para arrPoints {array} 控制点坐标集合
 */
export function createBezierCurvePoints (n, arrPoints) {
  let Ptx = 0
  let Pty = 0

  let arrbline = []
  for (let t = 0; t < 1; t = t + 0.01) {
    Ptx = 0
    Pty = 0
    for (let i = 0; i <= n; i++) {
      Ptx += (factorial(n) / (factorial(i) * factorial(n - i))) * Math.pow((1 - t), n - i) * Math.pow(t, i) * arrPoints[i][0]
      Pty += (factorial(n) / (factorial(i) * factorial(n - i))) * Math.pow((1 - t), n - i) * Math.pow(t, i) * arrPoints[i][1]
    }

    arrbline.push([Ptx, Pty])
  }
  return arrbline
}
